package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.dingdanDao;

/**
 * Servlet implementation class userAdd
 */
@WebServlet("/userAdd")
public class userAdd extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public userAdd() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");//防止出现中文乱码
		
		// 在另一个页面中获取保存的用户名信息
		HttpSession session = request.getSession();
		String savedUsername = (String) session.getAttribute("username");
	
		// 获取注册信息

		String dd_commodity =request.getParameter("useraddA");
		String dd_number =request.getParameter("useraddB");
		


		
		System.out.println(savedUsername);
		System.out.println(dd_commodity);
		System.out.println(dd_number);
		

		boolean a = dingdanDao.add(savedUsername,dd_commodity,dd_number);
		if(a==true){
			request.getSession().setAttribute("B1","添加成功");
			response.sendRedirect("/aaa/userindex.jsp");
		}else {
			request.getSession().setAttribute("B1","商品不存在，订单添加失败");
			response.sendRedirect("/aaa/userindex.jsp");	
		}
		

		

	}

}
